# 生成随机n位验证码的函数
import random

from qcloudsms_py import SmsSingleSender
from qcloudsms_py.httpclient import HTTPError
from . import settings  # 使用相对导入


def get_code(n=4):
    code = ''
    for i in range(n):
        code += str(random.randint(0, 9))
    return code


# 发送短信的函数
def send_sms(phone, code):
    phone_numbers = [phone, ]
    ssender = SmsSingleSender(settings.APPID, settings.APPKEY)
    params = [code]  # 当模板没有参数时，`params = []`
    try:
        result = ssender.send_with_param(86, phone_numbers[0],
                                         settings.TEMPLATE_ID, params, sign=settings.SMS_SIGN, extend="", ext="")
    except Exception as e:
        return False
    return True


if __name__ == '__main__':
    print(get_code())
